package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.enums.AppointmentStatusEnum;
import cn.wolfcode.enums.ConsumptionStatusEnum;
import cn.wolfcode.mapper.AppointmentMapper;
import cn.wolfcode.mapper.ConsumptionItemMapper;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.UserContextUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ConsumptionServiceImpl implements IConsumptionService {

    @Autowired
    private ConsumptionMapper consumptionMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private ConsumptionItemMapper consumptionItemMapper;


    @Override
    public void save(Consumption consumption) {
        consumption.setCreateTime(new Date());
        consumption.setCreateUser(UserContextUtil.getCurrentEmployee());
        consumption.setStatus(ConsumptionStatusEnum.CONSUME.getValue());
        String cno = DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomNumbers(5);
        consumption.setCno(cno);
        consumptionMapper.insert(consumption);
    }

    @Override
    public void delete(Long id) {
        consumptionItemMapper.deleteCno(consumptionMapper.queryByCno(id));
        consumptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Consumption consumption) {
        consumptionMapper.updateByPrimaryKey(consumption);
    }

    @Override
    public Consumption get(Long id) {
        return consumptionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Consumption> listAll() {
        return consumptionMapper.selectAll();
    }

    @Override
    public PageInfo<Consumption> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Consumption> consumptions = consumptionMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Consumption>(consumptions);
    }

    @Override
    public Long saveAppointment(Long aid) {
        appointmentMapper.checkStatus(aid, AppointmentStatusEnum.CONSUME.getValue());
        Appointment appointment = appointmentMapper.selectByPrimaryKey(aid);
        Consumption consumption = new Consumption();
        String cno = DateUtil.format(new Date(), "yyyyMMddHHmmss") + RandomUtil.randomNumbers(5);
        consumption.setCno(cno);
        consumption.setStatus(ConsumptionStatusEnum.CONSUME.getValue());
        consumption.setCustomerName(appointment.getContactName());
        consumption.setCustomerTel(appointment.getContactTel());
        consumption.setAppointmentAno(appointment.getAno());
        consumption.setBusiness(appointment.getBusiness());
        consumption.setCreateTime(new Date());
        consumption.setCheckinTime(new Date());
        consumption.setCreateUser(UserContextUtil.getCurrentEmployee());
        consumptionMapper.insert(consumption);
        return consumption.getId();
    }

    @Override
    public List<ConsumptionItem> queryByConsumptionItemItem(Long id) {
        String cno = consumptionMapper.queryByCno(id);
        return consumptionItemMapper.queryByConsumptionItemItem(cno);
    }

    @Override
    public void consume(Consumption consumption) {
        consumption.setPayTime(new Date());
        consumption.setPayee(UserContextUtil.getCurrentEmployee());
        consumption.setStatus(ConsumptionStatusEnum.AUDIT.getValue());
        consumptionMapper.consume(consumption);
    }

    @Override
    public void audit(Consumption consumption) {
        consumption.setAuditor(UserContextUtil.getCurrentEmployee());
        consumption.setAuditTime(new Date());
        consumption.setStatus(ConsumptionStatusEnum.FINISH.getValue());
        consumptionMapper.audit(consumption);
    }
}
